<?php

/**
 * @author ntourne
 * @date 23/05/2011
 * Template of an Entity class
 */

require_once(HOME_PATH.'class/class.DBManager.php');
require_once(HOME_PATH.'class/class.DBUtil.php');
require_once(HOME_PATH.'class/class.Security.php');
require_once(HOME_PATH.'class/class.ParameterDB.php');
require_once(HOME_PATH.'class/class.Validation.php');
require_once(HOME_PATH.'class/class.Status.php');
require_once(HOME_PATH.'class/class.ConfigManager.php');


class Entity {

	var $id = null;
	var $lastIpAddress = null;
	var $creationIpAddress = null;
	var $messages = null;
	var $recaptcha_challenge_field = null;
	var $recaptcha_response_field = null;
	var $recaptcha_publickey = null;
	var $recaptcha_privatekey = null;
	
	function __construct() {
		$this->messages = array();
		$this->lastIpAddress = $this->getIPAddress();
		$this->creationIpAddress = $this->lastIpAddress;
		$configManagerInst = new ConfigManager();
		$this->recaptcha_publickey = $configManagerInst->recaptcha_publickey;
		$this->recaptcha_privatekey = $configManagerInst->recaptcha_privatekey;
	} 
	
	
	/**
	 * Validate input data for a new Entity
	 * @return true if Entity's data validates
	 */
	function validate() {
		return true;
	}
	
	
	/**
	 * Get last inserted ID
	 * @return last ID
	 */
	function getLastInsertID($conn) {
		$sql = mysql_query("SELECT LAST_INSERT_ID()", $conn) or die (mysql_error());
		$row = mysql_fetch_array($sql);
		return (int)$row[0];
	}
	
	
	/**
	 * Get IP address
	 * @return IP address
	 */
	function getIPAddress() {
		return $_SERVER['REMOTE_ADDR'];
	}

	
	/**
	 * Generate a new token based on datetime+rand()
	 */
	function generateToken() {
		return md5(date('Y-m-d H:i:s')) . rand();
	}

	
	/**
	 * Add a new message
	 */
	function addMessage($message) {
		$this->messages[] = array("type" => $message[type], "text" => $message[text], "code" => $message[code]);		
	}
	
}